#!/bin/bash
dizhi_="$2" #执行文件地址
time_ss=`date "+%Y-%m-%d %H:%M:%S"`
#去空格
for file in `find ${dizhi_} -name "*.sql" | tr ' ' '#'` 
do
if [[ $file =~ "#" ]];then
mv "`echo $file | sed 's/#/ /g'`" "`echo $file | sed 's/#//g'`"
 fi            
done
#日志目录创建
for didi in $1  
do
if [ ! -d "/mnt/e/sh/log/$didi" ]; then  
  mkdir -p "/mnt/e/sh/log/$didi"
fi  
done

wen_=$(find ${dizhi_} -type d -name "hk_erp2*" -o -name "finedb" -o -name "hk_mid"|sort) 
for dizhi in $wen_
do

if [[ $dizhi =~ "finedb" ]]
then
    data='finedb'
else
    xiugai="${dizhi_}hk_erp2_"
    data=$(echo "$dizhi"|sed "s#$xiugai##g")
	data="\_$data"
fi

if [[ $dizhi =~ "hk_mid" ]]
then
    data='hk_mid'
fi

if [[ $1 == "bs_test" ]]
then
    data="${data}_test"
fi

xx=$(find $dizhi -name "*.sql"|sort)
cc=$(mysql --defaults-extra-file=/mnt/e/mysql/sh/test -D hk_dba -e"SELECT CONCAT('db-',LPAD(IFNULL(MAX(id),0)+1, 9, '0')) AS bill_no FROM dba_log;"|grep -v "bill_no")
server_="$1" #执行的数据库实例
for server_1 in $server_  
do

if [ "$1" == "g2" ]
then
case $3 in 
1)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "fs\_|fy\_") #执行的数据库
;;
2)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "qgzz\_|zclx\_") #执行的数据库
;;
3)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "pekd\_|sme3\_") #执行的数据库
;;
4)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "sme4\_|sme5\_|\_tenant|zj\_hk") #执行的数据库
;;
5)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "dk\_|kls\_") #执行的数据库
;;
6)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "sme2\_|sme6\_") #执行的数据库
;;
7)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "361\_|cdws\_") #执行的数据库
;;
8)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "sme\_") #执行的数据库
;;
*)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database") #执行的数据库
;;
esac
else 
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E 'hk_erp2|finedb') #执行的数据库
fi

for db_sql in $xx  
do
for db_1 in $db_  
do
id_=$(mysql --defaults-extra-file=/mnt/e/mysql/sh/test -D hk_dba -e"INSERT INTO dba_log(server_ip,database_name,guid, dizhi, msg,create_time)VALUE('$server_1','$db_1','$cc','$db_sql','',now(3));SELECT LAST_INSERT_ID() AS id"|grep -v "id")
mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -D $db_1 -A --compression-algorithms=zlib --comments <$db_sql > /mnt/e/sh/log/$server_1/$cc 2>&1
time_=`date +%Y-%m-%d\ %H:%M:%S.%3N`
msg=$(cat /mnt/e/sh/log/$server_1/$cc) #问题排查
if [ "$msg" != "" ]
then
  msg_=$(echo '"'"$msg"'"')
  mysql --defaults-extra-file=/mnt/e/mysql/sh/test -D hk_dba -e"update dba_log set msg=$msg_ ,stats=1,modified_time='$time_' where id=$id_ ;"
else 
  mysql --defaults-extra-file=/mnt/e/mysql/sh/test -D hk_dba -e"update dba_log set modified_time='$time_' where id=$id_ ;"
fi
done
done

done
done

#mysql --defaults-extra-file=/mnt/e/mysql/sh/test -D hk_dba -e"call sp_zhix('$time_ss')">/mnt/e/sh/log/mysqlzxerror
find /mnt/e/sh/log/ -name "db-*" -mtime +1 -exec rm -rf {} \;

